% Load the parameters for the calibration version of the model
%
% This is similar to the baseline model except for the macro side that
% features non-capitalized income that accrues to HTM agents
% Parameter n: share of noncapitalized income to capital income

clear;
global g rho sigma PStar taub tauh k l tau tauBarBench zStar demandImpactBench elasticityBench
% Share of non-capitalized income
global n

% Calibrate the macro parameters
rho=0.045; % To match the MPC out of stock wealth in Chodorow-Reich et al.
PStar = exp(-rho)/(1-exp(-rho));
wealthToConsumptionTarget = 118/14.7; % Based on 2019 Q4 measures for the US
% Match the efficient wealthToConsumption in the model, PStar/(n+1), to the data
n = PStar/wealthToConsumptionTarget - 1;

% Calibrate the asset pricing parameters

% Target 0% interest rate---taking into account other factors that reduces rates
rfBench = 0.01;
bankRRA = 3.7; % From He et al, Table 5
taub = 1/bankRRA;
tauh=taub/10;

%realEstateWealth = 29928374; % 2019Q4
%totalWealth = 118294432; %2019Q4
%k= (totalWealth - realEstateWealth)/totalWealth; This gives 0.74. Round to 0.75
k = 0.75;

% Set leverage to match the relationship between loan losses and capital
% declines in the Fed's June 2020 stress tests
l = fsolve(@(lTilde) 1-lTilde/0.937 - 10/12*(1-lTilde),1);

% Set risk premium to target a pre-shock elasticity of 1
demandImpactBench = k*l/(tauh/(taub-tauh) + k*(1-l));
elasticityBench = 1;
rpBench = fsolve(@(rp) elasticityBench-1/rp +demandImpactBench,0.1);
g = rpBench + rfBench - rho;
tauBarBench =  taub*k*(1-l)+tauh*(1-k*(1-l));
sigma = (rpBench*tauBarBench)^0.5;
tau = taub*k+tauh*(1-k);
zStar = fsolve(@(z) tau - k*l/z*(taub-tauh) -  sigma^2/(rho+g),1);


% Check the condition for supply shocks to induce demand recession
temp = tauh/k/(taub-tauh);
% The condition boils down to
% z_0 < l/(temp+1)*(1+rpBench/(1-varphi))
intercept = l/(temp+1);
slope = rpBench*l/(temp+1);

% Solve for the threshold productivity for fully transitory shocks to trigger demand recessions
varphi = 0;
z0BarTransitoryFull= slope + intercept/(1-varphi);
% Solve the same thing for almost fully transitory shocks (varphi=0.2)
varphi = 0.15;
z0BarTransitoryPart= slope + intercept/(1-varphi);
% Solve for the persistence threshold such that any z0<=1 induces demand recession
varphiBar = 1-intercept/(1-slope);

%zEq = 0.92;
%zImpactEq = k*(l/zEq)/ (tauh/(taub-tauh) + k*(1-(l/zEq)));